package campus.aiit.ssl.service;

import campus.aiit.ssl.dao.BlockDao;
import campus.aiit.ssl.domain.model.Block;
import campus.aiit.ssl.manage.custom.dao.DaoProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;

@Service
public class BlockService {

    private BlockDao blockDao;

    @Autowired
    public BlockService(BlockDao blockDao) {
        this.blockDao = blockDao;
    }

    @PostConstruct
    public void afterConstruct() {
        DaoProxy<BlockDao> proxy = new DaoProxy<>();
        blockDao = proxy.getProxy(blockDao);
    }

    public int insertBlock(Block record) {
        return blockDao.insertSelective(record);
    }

    public int deleteByPrimaryKey(Long id) {
        blockDao.deleteBlockIdForRoom(id);
        return blockDao.deleteByPrimaryKey(id);
    }

    public Block[] selectByPrimaryKey(Long id) {
        return blockDao.selectById(id);
    }

    public int updateByPrimaryKeySelective(Block record){
        return blockDao.updateByPrimaryKeySelective(record);
    }

    public Block[] getBlocks(String userId){
        if(userId != null){
            Block[] blocks = blockDao.getBlocks(userId);
            Block[] blocks1 = blockDao.blockWithNullUser();
            Block[] result = java.util.Arrays.copyOf(blocks,blocks1.length+blocks.length);
            for (int i = 0;i<blocks1.length;i++){
                result[blocks.length+i] = blocks1[i];
            }
            return result;
        }
        return blockDao.getBlocks(null);
    }

    public int addRoomForBlock(Map map) {
        return blockDao.addRoomForBlock(map);
    }

    public int delRoomForBlock(Map map) {
        return blockDao.delRoomForBlock(map);
    }

    public Block[] getBlockByUserId(String userId) {
        return blockDao.getBlocks(userId);
    }

    public Block[] blockWithUserNull(){
        return blockDao.blockWithNullUser();
    }
}
